import { computed, getCurrentInstance } from 'vue'

/**
 * @description v-model
 * @param props props 接收的属性
 * @param key   v-model 的属性名
 * @param emit  事件
 * @returns
 */
export function useVmodel<T>(props: any, key = 'modelValue', emit?: any) {
	const vm = getCurrentInstance()
	const _emit = emit || vm?.emit
	const event = `update:${key}`
	return computed<T>({
		get() {
			return props[key]
		},
		set(newVal: any) {
			_emit(event, newVal)
		}
	})
}
